package com.assess.web.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListUtil {

    /**
     * list转为String
     *
     * @param list
     * @return
     */
    public static String ListToString(List<String> list) {
        String result = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                result = list.get(i);
            } else {
                result = result + "," + list.get(i);
            }
        }
        return result;
    }

    /**
     * 整数型list集合转换成字符串
     *
     * @param list
     * @return
     */
    public static String ListToIntChangeToString(List<Integer> list) {
        StringBuilder st = new StringBuilder();
        for (Integer id : list) {
            st.append(id).append(",");
        }
        return st.toString().substring(0, st.length() - 1);
    }

    /**
     * 字符串转换list
     *
     * @param str
     * @return
     */
    public static List<String> StringToList(String str) {
        String[] strArry = str.split(",");
        return Arrays.asList(strArry);
    }

    /**
     * Int集合转string集合list
     *
     * @param list
     * @return
     */
    public static List<String> ListChangeInteger(List<Integer> list) {
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            result.add(String.valueOf(list.get(i)));
        }
        return result;
    }


    /**
     * 字符串转换Int集合list
     *
     * @param str
     * @param delimiter
     * @return
     */
    public static List<Integer> splitToInt(String str, String delimiter) {
        String[] res = str.split(delimiter);
        List<Integer> ret = new ArrayList<>();
        for (String r : res) {
            ret.add(Integer.valueOf(r));
        }
        return ret;
    }
}
